<template>
  <XtxDialog :title="(formData.id ? '修改' : '添加') + '收货地址'" v-model:visiable="dialogVisible">
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input v-model="formData.contact" class="input" placeholder="请输入手机号" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity placeholder="请选择所在地区" :fullLocation="formData.fullLocation" @changeCity="changeCity" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right: 20px" @click="dialogVisible = false">取消</XtxButton>
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    // 控制地址框的显示和隐藏
    const dialogVisible = ref(false)
    const open = (address) => {
      if (!address.id) {
        // 在打开对话框的时候将表单元素清空(如果是添加的情况)
        for (const key in formData) {
          if (key === 'isDefault') {
            formData[key] = 1
          } else {
            formData[key] = null
          }
        }
      } else {
        // 如果是修改地址的逻辑  填充表单
        for (const key in formData) {
          formData[key] = address[key]
        }
      }
      dialogVisible.value = true
    }

    // 表单元素的数据(双向绑定)  其中的fullLocation是传递给城市组件的
    const formData = reactive({
      id: '',
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 0,
      fullLocation: ''
    })

    // 城市组件自定义事件
    const changeCity = (city) => {
      console.log(city)
      formData.provinceCode = city.provinceCode
      formData.cityCode = city.cityCode
      formData.countyCode = city.countyCode
      formData.fullLocation = city.fullLocation
    }

    // 提交信息
    const submit = () => {
      // 判断是修改还是添加的情况
      if (formData.id) {
        // 修改的情况
        editAddress(formData).then((data) => {
          // 提交成功之后关闭对话框，向父组件提交子组件信息
          Message({ type: 'success', text: '添加成功' })
          dialogVisible.value = false
          emit('onSuccess', formData)
        })
      } else {
        addAddress(formData).then((data) => {
          // 提交成功之后关闭对话框，向父组件提交子组件信息
          Message({ type: 'success', text: '添加成功' })
          dialogVisible.value = false
          formData.id = data.result.id
          emit('onSuccess', formData)
        })
      }
    }
    return { dialogVisible, open, formData, changeCity, submit }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
